package com.blueocean_health.care.management.service.impl;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.CashRecord;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.CashRecordTableDto;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.em.PurposeEnum;
import com.blueocean_health.care.management.em.TransTypeEnum;
import com.blueocean_health.care.management.mapper.CashAccountMapper;
import com.blueocean_health.care.management.mapper.CashRecordMapper;
import com.blueocean_health.care.management.mapper.UserMapper;
import com.blueocean_health.care.management.service.CashRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 资金管理-日记账
 * @author wuyanli
 *
 */
@Service
public class CashRecordServiceImpl implements CashRecordService{
	
	@Autowired
	private CashRecordMapper cashDao;
	
	@Autowired
	private UserMapper userDao;
	
	@Autowired
	private CashAccountMapper accountDao;
	
	DecimalFormat df = new DecimalFormat("0.00");

	@Override
	public int addCashRecord(CashRecord cashRecord) {
		String purpose = cashRecord.getPurpose();
		//资金用途  根据下拉表 选择的Id得到对应的中文名称 
		String val2 = PurposeEnum.valueOf("_"+purpose).getVal();
		cashRecord.setPurposeName(val2);
		cashRecord.setFee(-cashRecord.getFee());
		//资金类型  根据下拉表 选择的Id得到对应的中文名称 
		String transType = cashRecord.getTransType();
		String val = TransTypeEnum.valueOf("_"+transType).getVal();
		cashRecord.setTransName(val);
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		cashRecord.setHospitalId(user.getHospitalId());
		//校验银行流水号  2017年12月1日09:53:00  
		//去除 订单流水校验 2018年3月22日15:56:02
//		Map<String,String> map = new HashMap<>();
//		map.put("receiptNum", cashRecord.getReceiptNum().trim());
//		List<CashRecord> cashRecordByReceNum = getCashRecordByReceNum(map);
//		int addCashRecord = 0;
//		if(StringUtils.isEmpty(cashRecordByReceNum) || cashRecordByReceNum.size() == 0){
			int addCashRecord = cashDao.addCashRecord(cashRecord);
			return addCashRecord;
//			return 1;
//		}
//		else if(cashRecordByReceNum.size() > 0){
//			return 999999;
//		}
//		return addCashRecord;
	}

	@Override
	public int updateCashRecordById(CashRecord cashRecord) {
		String purpose = cashRecord.getPurpose();
		if(PurposeEnum._4.getKey().equals(purpose)){
			cashRecord.setFee(-cashRecord.getFee());
		}
		String val = TransTypeEnum.valueOf("_"+cashRecord.getTransType()).getVal();
		cashRecord.setTransName(val);
		//校验银行流水号  2017年12月1日09:53:00
		//去除 订单流水校验 2018年3月22日15:56:02
//		Map<String,String> map = new HashMap<>();
//		String receiptNum = cashRecord.getReceiptNum();
//		if(!StringUtils.isEmpty(receiptNum)){
//			receiptNum = receiptNum.trim();
//			cashRecord.setReceiptNum(receiptNum);
//		}
//		map.put("receiptNum", receiptNum);
//		map.put("id", cashRecord.getId()+"");
//		List<CashRecord> cashRecordByReceNum = getCashRecordByReceNum(map);
//		int updateCashRecordById = 0;
//		if(StringUtils.isEmpty(cashRecordByReceNum) || cashRecordByReceNum.size() == 0){
			int updateCashRecordById = cashDao.updateCashRecordById(cashRecord);
			return updateCashRecordById;
//		}
//		else if(cashRecordByReceNum.size() > 0){
//			return 999999;
//		}
//		return updateCashRecordById;
	}

	@Override
	public int deleteCashRecordById(int id) {
		//逻辑删除操作
		int deleteCashRecordById = cashDao.deleteCashRecordById(id);
		return deleteCashRecordById;
	}

	@Override
	public PageInfo<CashRecord> getAllCashRecord(Map<String, Object> map) {
		//金额范围如果没填 默认最小为0 最大为double最大值
		if(map.get("feeMin") == null || "".equals(map.get("feeMin"))){
			map.put("feeMin", -Double.MAX_VALUE);
		}
		if(map.get("feeMax") == null || "".equals(map.get("feeMax"))){
			map.put("feeMax", Double.MAX_VALUE);
		}
		if(StringUtils.isEmpty(map.get("startTime"))){
			map.put("startTime", "1900-01-01 00:00:00");
		}else{
			map.put("startTime", map.get("startTime")+" 00:00:00");
		}
		if(StringUtils.isEmpty(map.get("endTime"))){
			map.put("endTime", "2500-01-01 00:00:00");
		}else{
			map.put("endTime", map.get("endTime")+" 23:59:59");
		}
		User user1 = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user1.getHospitalId());
		int pageSize = Integer.parseInt(map.get("pageSize")+"");
		int page = Integer.parseInt(map.get("page")+"");
		//根据负责人进行筛选
		if(StringUtils.isEmpty(map.get("manager"))){
			map.put("manager", "");
		}else{
			String userId = map.get("manager")+"";
			User user = userDao.getUserByUserId(userId);
			String userRealName = user.getUserRealName();
			map.put("manager", userRealName);
		}
		PageHelper.startPage(page,pageSize);
		List<CashRecord> allCashRecord = cashDao.getAllCashRecord(map);
		PageInfo<CashRecord> pageInfo = new PageInfo<CashRecord>(allCashRecord);
		return pageInfo;
	}

	/**
	 * 现金日记账余额表 2017年8月22日18:09:34 修改时间  2017年9月21日14:37:21
	 */
	@Override
	public List<CashRecordTableDto> getCashBalanceTable(Map<String, String> map) {
		
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		map.put("hospitalId", user.getHospitalId());
//		map.put("hospitalId", "1");
		map.put("startTime",map.get("startTime")+" 00:00:00");
		map.put("endTime",map.get("endTime")+" 23:59:59");
		//预收金额，续费金额，退款金额，存款金额，本期余额
		List<CashRecordTableDto> cashBalanceTable = cashDao.getCashBalanceTable(map);
		//期初余额 2017年9月21日14:49:30
		List<CashRecordTableDto> beginCashBalance = cashDao.getBeginCashBalance(map);
		//期末余额 2017年9月21日14:49:33
		List<CashRecordTableDto> endCashBalance = cashDao.getEndCashBalance(map);
		//添加总计对象
		CashRecordTableDto cashTable = new CashRecordTableDto();
		cashTable.setTransName("总计");
		Double advanceCount = 0d;
		Double depositCount = 0d;
		Double drawbackCount = 0d;
		Double renewalCount = 0d;
		Double balanceCount = 0d;
		Double beginBalanceCount = 0d;
		Double endBalanceCount = 0d;
		//将期初余额和期末余额都封装到一个对象
		for (int i = 0; i < cashBalanceTable.size(); i++) {
			cashBalanceTable.get(i).setBeginBalance(beginCashBalance.get(i).getBeginBalance());
			cashBalanceTable.get(i).setEndBalance(endCashBalance.get(i).getEndBalance());
//			if("0.00".equals(cashBalanceTable.get(i).getDrawback())){
//				cashBalanceTable.get(i).setDrawback(cashBalanceTable.get(i).getDrawback());
//			}else{
//				cashBalanceTable.get(i).setDrawback("-"+cashBalanceTable.get(i).getDrawback());
//			}
		}
		//通过遍历 计算各种资金用途的总计金额
		for (CashRecordTableDto cashRecordTableDto : cashBalanceTable) {
			advanceCount += Double.parseDouble(cashRecordTableDto.getAdvance());
			depositCount += Double.parseDouble(cashRecordTableDto.getDeposit());
			drawbackCount += Double.parseDouble(cashRecordTableDto.getDrawback());
			renewalCount += Double.parseDouble(cashRecordTableDto.getRenewal());
			balanceCount += Double.parseDouble(cashRecordTableDto.getBalance());
		}
		
		//期初余额 总计 2017年9月21日14:55:01
		for (CashRecordTableDto cashRecordTableDto : beginCashBalance) {
			beginBalanceCount += Double.parseDouble(cashRecordTableDto.getBeginBalance());
		}
		//期末余额 总计 2017年9月21日14:55:03
		for (CashRecordTableDto cashRecordTableDto : endCashBalance) {
			endBalanceCount += Double.parseDouble(cashRecordTableDto.getEndBalance());
		}
		//总计
		
		cashTable.setAdvance(df.format(advanceCount));
		cashTable.setDeposit(df.format(depositCount));
		cashTable.setDrawback(df.format(drawbackCount));
		cashTable.setRenewal(df.format(renewalCount));
		cashTable.setBalance(df.format(balanceCount));
		cashTable.setBeginBalance(df.format(beginBalanceCount));
		cashTable.setEndBalance(df.format(endBalanceCount));
		cashBalanceTable.add(cashTable);
		
		return cashBalanceTable;
	}

	@Override
	public CashRecord getCashRecordById(int id) {
		//主要用于修改前的查询对象
		CashRecord cashRecordById = cashDao.getCashRecordById(id);
		Double fee = cashRecordById.getFee();
		if(fee < 0){
			cashRecordById.setFee(-fee);
		}
		return cashRecordById;
	}

	/**
	 * 校验银行流水号的唯一性  2017年11月30日16:46:23
	 */
	@Override
	public List<CashRecord> getCashRecordByReceNum(Map<String,String> map) {
		String receiptNum = map.get("receiptNum");
		
		String id = map.get("id");
		if(StringUtils.isEmpty(id)){
			List<CashRecord> cashRecordByReceNum = cashDao.getCashRecordByReceNum(receiptNum);
			return cashRecordByReceNum;
		}else{
			CashRecord cashRecordById = cashDao.getCashRecordById(Integer.parseInt(id));
			if(receiptNum.equals(cashRecordById.getReceiptNum())){
				return null;
			}else{
				List<CashRecord> cashRecordByReceNum = cashDao.getCashRecordByReceNum(receiptNum);
				return cashRecordByReceNum;
			}
		}
		
	}

	@Override
	public List<PullDownDto> accountList() {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		List<PullDownDto> accountList = accountDao.accountList(user.getHospitalId());
		return accountList;
	}

}
